package ui;
import java.awt.event.ActionEvent;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import dao.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class searchWindow extends javax.swing.JFrame {
	private boolean behavior = false;
	private static boolean filled;
	private BirdVO[] blist;
	private JPanel jPanel1;
	private JTextField searchScientificNameField;
	private JButton searchOkButton;
	private AbstractAction abstractAction1;
	private AbstractAction asearchCloseAction;
	private JTextField searchPopularNameField;
	private JLabel jLabel2;
	private JLabel jLabel1;
	
	public searchWindow() {
		super();
		initGUI();
	}
	
	public void setSearchBehavior() {
		behavior = false;
	}
	
	public void setUpdateBehavior() {
		behavior = true;
	}
	
	private void initGUI() {
		Vector<String> hlist = new Vector<String>();
		
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Search");
			{
				jPanel1 = new JPanel();
				GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				{
					jLabel1 = new JLabel();
					jLabel1.setText("Scientific Name:");
				}
				{
					searchOkButton = new JButton();
					searchOkButton.setText("OK");
					searchOkButton.setAction(getAsearchCloseAction());
				}
				{
					jLabel2 = new JLabel();
					jLabel2.setText("Popular Name:");
				}
				{
					searchScientificNameField = new JTextField();
				}
				{
					searchPopularNameField = new JTextField();
				}
				jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addComponent(jLabel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(searchPopularNameField, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addComponent(searchScientificNameField, GroupLayout.PREFERRED_SIZE, 176, GroupLayout.PREFERRED_SIZE))
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addGap(90)
					        .addComponent(searchOkButton, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(48, Short.MAX_VALUE));
				jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(searchScientificNameField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(searchPopularNameField, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(20)
					.addComponent(searchOkButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(42, Short.MAX_VALUE));
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addComponent(jPanel1, 0, 270, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addComponent(jPanel1, 0, 390, Short.MAX_VALUE));
			pack();
			this.setSize(360, 148);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	public static BirdVO[] Search(String sciName , String popName) {
		BirdVO b = null, temp = null;
		Vector<BirdVO> bl = new Vector<BirdVO>();
		boolean flag = true;
		String boom = sciName;
		String boom2 = popName;
		filled = false;
		
		//Check if any of the two fields are filled
		//TODO: filter by scientific name, if field is filled.
		try {
			if ( boom.length() > 0 ) {
				b = BirdDAO.findByScientificName(boom);
				filled = true;
			} 
		
			if ( boom2 != null && boom2.length() > 0 ) {
				filled = true;
				for(BirdVO o: BirdDAO.findsAllBirdsByPopularName(boom2)) {
					temp = (BirdVO)o;
					bl.add(temp);
					
					// Does not allow duplicate entries
					if ( b != null && temp.getScientific_name() == b.getScientific_name() ) {
						flag = false;
					}
				}
			}
			
			if ( flag && b != null )
				bl.add(b);
		
		} catch (Throwable e) {
			System.out.println(e.getMessage());
			filled = true;
		};
		
		BirdVO[] blist = new BirdVO[bl.size()];
		
		//Stupid java does not allow explicit cast from arrays to
		//vector. Gotta manually iterate over each element and cast it.
		if ( bl.size() > 0 ) {
			blist = new BirdVO[bl.size()];
			
			for(int i = 0; i < bl.size(); i++) {
				blist[i] = (BirdVO)bl.elementAt(i);
			}
		}
		
		return blist;
	}
	
	private AbstractAction getAsearchCloseAction() {
		if(asearchCloseAction == null) {
			asearchCloseAction = new AbstractAction("Search", null) {
				public void actionPerformed(ActionEvent evt) {
					blist = Search(searchScientificNameField.getText(),searchPopularNameField.getText());
					if ( blist.length > 0 ) {
						
						// Call the search form and pass over the data.
						resultWindow rW = new resultWindow();
						if ( behavior )
							rW.setUpdateBehavior();
						else
							rW.setSearchBehavior();
						
						rW.setLocationRelativeTo(null);
						rW.setVisible(true);
						rW.insertData(blist);
					
					} else {
						successDialog sD = new successDialog(searchWindow.this);
						sD.pack();
						sD.setLocationRelativeTo(null);
						sD.setVisible(true);
						
						if ( !filled ) {
							//No field was filled. Can't search.
							sD.setLabelText("Fields are empty.");
						} else {
							//No entries have been found.
							sD.setLabelText("No matching bird was found.");
						}
					}
				}
			};
		}
		return asearchCloseAction;
	}

	private AbstractAction getAbstractAction1() {
		if(abstractAction1 == null) {
			abstractAction1 = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return abstractAction1;
	}

}
